#!/usr/bin/python
 # -*- coding: UTF-8 -*-
import urllib
import os
import stat

def downImg(src):
	f = src.split('/')[-1].split('?')[0]
	#print(f)
	r = urllib.urlretrieve(src, 'D:\\Temp\\img\\' + f)
	print('download img:%s'%(src))



def procImg(img):
	#print(img)
	srcI = img.find('src="')
	srcE = img.find('"', srcI + 5)
	downImg(img[srcI+5: srcE])


def procHtml(s):
	#print(s)
	bodyI = s.find('<body')
	imgI = s.find('<img', bodyI)
	endI = 0
	while imgI > -1:
		endI = s.find('>', imgI)
		if imgI > -1 and endI > -1:
			imgstr = s[imgI:endI]
			procImg(imgstr)
		else:
			print('imgI:%d' %(imgI))
			print('endI:%d' %(endI))
		imgI = s.find('<img', endI)


#更改工作目录
os.chdir('D:\\Temp\\img')
#更改权限 
os.chmod('D:\\Temp\\img', stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)

f = urllib.urlopen('http://www.cnblogs.com/')
c = f.getcode()
if c == 200:
	s = f.read()
	procHtml(s)
else:
	print('Request :%s'%(c))

print('game over')
